﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public  class PToolingChargeDAL
    {
        public static int IsExist(string ItemCode)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PToolingCharge_IsExist", ItemCode);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static int Add(PToolingChargeInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PToolingCharge_Add", obj.ItemCode, obj.ItemName, obj.SupplerName, obj.Price, obj.NewPrice, obj.NeedBuyNum, obj.ForwardNum, obj.AddRealName, obj.mouldCharge,obj.IsAdjust);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static int AddDetail(int PTCID,string ItemCode,string ItemName)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PToolingChargeDetail_Add", PTCID, ItemCode, ItemName);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static bool Delete(int ID)
        {
            try
            {
                Database DB = DatabaseFactory.CreateDatabase();

                DbCommand dbCommand = DB.GetStoredProcCommand("PToolingCharge_Delete", ID);
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetById(int ID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("PToolingCharge_GetById", ID);
            return DB.ExecuteReader(dbCommand);
	    }
        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("PToolingCharge_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String,WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(PToolingChargeInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PToolingCharge_Update", obj.ID, obj.SupplerName, obj.NewPrice, obj.NeedBuyNum, obj.ForwardNum, obj.IsAdjust, obj.mouldCharge, obj.ItemName, obj.Price);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 获取所有需提示的分摊项
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static IDataReader GetExpireList()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PToolingCharge_GetExpireList");
            return DB.ExecuteReader(dbCommand);
        }
    }
}
